#include "jsonfilehelper.h"
#include <QCoreApplication>
#include <QJsonObject>
#include <QJsonArray>
#include "src/Models/NormalDefectModels.h"

static QString Json_SteelType = "Steeltype";
static QString Json_Steel_Lv = "DefectLvDetect";
static QString Json_DefectName = "DefectName";
static QString Json_DefectStandrd = "DefectStandrd";

static QString Json_Defect_Name= "name";
static QString Json_Defect_Length= "length";
static QString Json_Defect_Width= "width";
static QString Json_Defect_Times= "times";

bool JsonFileHelper::ReadJsonFile(QString jsonfileName,NormalDetectBase& normaldefect,
                                  VerticalLengthDetectBase& verticallength,
                                  FrequencyDetectBase& frequenceDetect,
                                  DirectLevelBase& directDetect,
                                  BesideDetectBase& besideDetect)
{
    QString fileName = QCoreApplication::applicationDirPath()+"/" +jsonfileName;

    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text )){
        qWarning() << "读取不到json文件" << file.errorString();
        return false;
    }
    QByteArray jsonData =  file.readAll();
    file.close();


    QJsonDocument doc =  QJsonDocument::fromJson(jsonData);
    if(doc.isNull() || !doc.isObject()){
        qDebug()<<"Json文件解析失败！";
        return false;
    }
    QJsonObject rootObj = doc.object();

    if(rootObj.contains("NormalDetect")){
        JsonParseToObj(doc,"NormalDetect",normaldefect);
    };
    if(rootObj.contains("VerticalLengthDetect")){
        JsonParseToObj(doc,"VerticalLengthDetect",verticallength);
    };

    if(rootObj.contains("FrequencyDetect")){
        JsonParseToObj(doc,"FrequencyDetect",frequenceDetect);
    };

    if(rootObj.contains("DirectLevel")){
         JsonParseToObj(doc,"DirectLevel",directDetect);
    };

    if(rootObj.contains("BesideDetect")){
         JsonParseToObj(doc,"BesideDetect",besideDetect);
    };

    return true;
}

bool JsonFileHelper::WriteJsonFile(QString jsonfileName)
{
    QString filepath = QCoreApplication::applicationDirPath()+"/" +jsonfileName;

    QJsonObject RootObj;

    //长宽判断方式
    {
        QJsonArray NormalDetectArr;
        QJsonObject DefectLvDetectObj;
        QJsonArray DefectLvDetectArr;
        QJsonObject DefectStandrdObj;
        QJsonArray DefectStandrdArr;
        DefectLvDetectObj[Json_SteelType] = "示例钢种";
        DefectStandrdObj[Json_DefectName] = "示例缺陷名称";
        QJsonObject SubDefectStandrd1;
        SubDefectStandrd1[Json_Defect_Name] = "一级";
        SubDefectStandrd1[Json_Defect_Length] = 0;
        SubDefectStandrd1[Json_Defect_Width] = 0;
        SubDefectStandrd1[Json_Defect_Times] = 0;
        QJsonObject SubDefectStandrd2;
        SubDefectStandrd2[Json_Defect_Name] = "二级";
        SubDefectStandrd2[Json_Defect_Length] = 0;
        SubDefectStandrd2[Json_Defect_Width] = 0;
        SubDefectStandrd2[Json_Defect_Times] = 0;
        QJsonObject SubDefectStandrd3;
        SubDefectStandrd3[Json_Defect_Name] = "三级";
        SubDefectStandrd3[Json_Defect_Length] = 0;
        SubDefectStandrd3[Json_Defect_Width] = 0;
        SubDefectStandrd3[Json_Defect_Times] = 0;
        DefectStandrdArr.append(SubDefectStandrd1);
        DefectStandrdArr.append(SubDefectStandrd2);
        DefectStandrdArr.append(SubDefectStandrd3);
        DefectStandrdObj[Json_DefectStandrd] = DefectStandrdArr;
        DefectLvDetectArr.append(DefectStandrdObj);
        DefectLvDetectObj[Json_Steel_Lv] = DefectLvDetectArr;
        NormalDetectArr.append(DefectLvDetectObj);

        RootObj["NormalDetect"] = NormalDetectArr;
    }

    {
        //纵向判断方式
        QJsonArray LengthDetectArr;
        QJsonObject LengthDetectObj;
        QJsonObject Child_DetectObj;
        QJsonObject Child_Child_DetectObj;
        QJsonArray Child_Child_DetectArr;
        QJsonObject Child_Child_Child_DetectObj;
        QJsonArray Child_Child_Child_DetectArr;
        QJsonObject Child_Child_Child_Child_DetectObj1;
        QJsonObject Child_Child_Child_Child_DetectObj2;
        QJsonObject Child_Child_Child_Child_DetectObj3;
        Child_Child_Child_Child_DetectObj1[Json_Defect_Name] ="一级";
        Child_Child_Child_Child_DetectObj1[Json_Defect_Length] = 0;
        Child_Child_Child_Child_DetectObj2[Json_Defect_Name] ="二级";
        Child_Child_Child_Child_DetectObj2[Json_Defect_Length] = 0;
        Child_Child_Child_Child_DetectObj3[Json_Defect_Name] ="三级";
        Child_Child_Child_Child_DetectObj3[Json_Defect_Length] = 0;
        Child_Child_Child_DetectArr.append(Child_Child_Child_Child_DetectObj1);
        Child_Child_Child_DetectArr.append(Child_Child_Child_Child_DetectObj2);
        Child_Child_Child_DetectArr.append(Child_Child_Child_Child_DetectObj3);
        Child_Child_Child_DetectObj[Json_DefectName] = "示例缺陷";
        Child_Child_Child_DetectObj[Json_DefectStandrd] = Child_Child_Child_DetectArr;
        Child_Child_DetectArr.append(Child_Child_Child_DetectObj);
        Child_DetectObj[Json_SteelType] = "示例钢种";
        Child_DetectObj[Json_Steel_Lv] = Child_Child_DetectArr;
        LengthDetectArr.append(Child_DetectObj);

        RootObj["VerticalLengthDetect"] = LengthDetectArr;
    }

    {
        //根据缺陷距离边部多少来判断
        QJsonArray BesideDetecctArr;
        QJsonObject BesideDetecctObj;
        QJsonArray Child_Arr;
        QJsonObject Child_Obj;
        QJsonArray Child_Child_Arr;
        QJsonObject Child_Child_Obj1;
        QJsonObject Child_Child_Obj2;
        QJsonObject Child_Child_Obj3;

        Child_Child_Obj1[Json_Defect_Name] = "一级";
        Child_Child_Obj1[Json_Defect_Length] = 0;
        Child_Child_Obj2[Json_Defect_Name] = "二级";
        Child_Child_Obj2[Json_Defect_Length] = 0;
        Child_Child_Obj3[Json_Defect_Name] = "三级";
        Child_Child_Obj3[Json_Defect_Length] = 0;

        Child_Child_Arr.append(Child_Child_Obj1);
        Child_Child_Arr.append(Child_Child_Obj2);
        Child_Child_Arr.append(Child_Child_Obj3);

        Child_Obj[Json_DefectName] = "示例缺陷名称";
        Child_Obj[Json_DefectStandrd] = Child_Child_Arr;

        Child_Arr.append(Child_Obj);
        BesideDetecctObj[Json_SteelType] = "示例钢种";
        BesideDetecctObj[Json_Steel_Lv] = Child_Arr;

        BesideDetecctArr.append(BesideDetecctObj);
        RootObj["BesideDetect"] = BesideDetecctArr;
    }

    {
        //多少米内出现的频率
        QJsonArray FrequencyDetectArr;
        QJsonObject FrequencyDetectObj;
        QJsonArray Child_Arr;
        QJsonObject Child_Obj;
        QJsonArray Child_Child_Arr;
        QJsonObject Child_Child_Obj1;
        QJsonObject Child_Child_Obj2;
        QJsonObject Child_Child_Obj3;

        Child_Child_Obj1[Json_Defect_Name] = "一级";
        Child_Child_Obj1[Json_Defect_Length] = 0;
        Child_Child_Obj1[Json_Defect_Times] = 0;
        Child_Child_Obj1[Json_Defect_Width] = 0;
        Child_Child_Obj2[Json_Defect_Name] = "二级";
        Child_Child_Obj2[Json_Defect_Times] = 0;
        Child_Child_Obj2[Json_Defect_Length] = 0;
        Child_Child_Obj1[Json_Defect_Width] = 0;
        Child_Child_Obj3[Json_Defect_Name] = "三级";
        Child_Child_Obj3[Json_Defect_Times] = 0;
        Child_Child_Obj3[Json_Defect_Length] = 0;
        Child_Child_Obj1[Json_Defect_Width] = 0;

        Child_Child_Arr.append(Child_Child_Obj1);
        Child_Child_Arr.append(Child_Child_Obj2);
        Child_Child_Arr.append(Child_Child_Obj3);

        Child_Obj[Json_DefectName] = "示例缺陷名称";
        Child_Obj[Json_DefectStandrd] = Child_Child_Arr;
        Child_Arr.append(Child_Obj);
        FrequencyDetectObj[Json_SteelType] = "示例钢种";
        FrequencyDetectObj[Json_Steel_Lv] = Child_Arr;
        FrequencyDetectArr.append(FrequencyDetectObj);

        RootObj["FrequencyDetect"] =FrequencyDetectArr;
    }

    {
        //直接判级
        QJsonArray DirectLevelArr;
        QJsonObject DirectLevelObj;
        QJsonArray Child_Arr;
        QJsonObject Child_Obj;
        Child_Obj[Json_DefectName] = "示例缺陷名称";
        Child_Arr.append(Child_Obj);
        DirectLevelObj[Json_SteelType] = "示例钢种";
        DirectLevelObj[Json_Steel_Lv] = Child_Arr;
        DirectLevelArr.append(DirectLevelObj);

        RootObj["DirectLevel"] = DirectLevelArr;
    }

    QJsonDocument doc(RootObj);

    QFile file(filepath);
    if (!file.open(QIODevice::WriteOnly  | QIODevice::Text)) {
        qWarning() << "写入json文件失败" << file.errorString();
        return false;
    }
    file.write(doc.toJson(QJsonDocument::Indented));

    return true;
}

void JsonFileHelper::JsonParseToObj(const QJsonDocument &doc,QString objName,
                                    NormalDetectBase& detectbase)
{


    QJsonObject rootObj = doc.object();
    // JsonParseToObj(doc,"FrequencyDetect",frequenceDetect);
    if(rootObj.contains(objName) && rootObj[objName].isArray()){
        QJsonArray arr =  rootObj[objName].toArray(); // "FrequencyDetect": [

        for(const QJsonValue &value: arr) {

            NormalDetectWrapper *normalDetectWrapper = new NormalDetectWrapper; //钢种对象


            if(value.isObject()){
                const QJsonObject &obj = value.toObject();
                if(!obj[Json_SteelType].isNull()){
                    auto steelType = obj[Json_SteelType].toString();

                    normalDetectWrapper->SetSteelType(steelType);

                    if(obj[Json_Steel_Lv].isArray() && !obj[Json_Steel_Lv].isNull()){
                        //开始遍历缺陷名称和缺陷条件的设置
                        for(const QJsonValue &ele :obj[Json_Steel_Lv].toArray()){

                            DefectLvDetectWrapper *wrapper =new DefectLvDetectWrapper;//1，2，3级对象

                            if(ele.isObject() && !ele.isNull()){
                                const QJsonObject &eleobj = ele.toObject();
                                if(eleobj.contains(Json_DefectName)){
                                    auto defectName = eleobj[Json_DefectName].toString();
                                    wrapper->SetDefectName(defectName);

                                    if(eleobj[Json_DefectStandrd].isArray()){
                                        const QJsonArray & childarr= eleobj[Json_DefectStandrd].toArray();

                                        for(const QJsonValue &childValue :childarr) { //遍历的1，2，3级条件和等级名称
                                            QString name = childValue[Json_Defect_Name].toString();
                                            int length = childValue[Json_Defect_Length].toInt();
                                            int width = childValue[Json_Defect_Width].toInt();
                                            int times = childValue[Json_Defect_Times].toInt();

                                            wrapper->addDefectStandrd(new DefectStandrd(name,length,width,times));
                                        }
                                        //这里运行完毕就是疤痕 1 2 3
                                    }
                                }
                            }
                            normalDetectWrapper->AddDefectLvDetect(wrapper);
                        }
                    }
                }
            }

            detectbase.AddNormalDetectWrapper(normalDetectWrapper);
        }

    };
}

